package com.alipay.app.groupmeal.standard.mqtt;

import android.os.Environment;

import com.alipay.app.groupmeal.standard.constant.ConfigConstant;
import com.tencent.iot.hub.device.java.core.log.TXMqttLogCallBack;

/**
 * 实现TXMqttLogCallBack回调接口
 */
public class SelfMqttLogCallBack extends TXMqttLogCallBack {

    @Override
    public String setSecretKey() {
        String secertKey;
        if (ConfigConstant.devPSK != null && ConfigConstant.devPSK.length() != 0) {  //密钥认证
            secertKey = ConfigConstant.devPSK;
            secertKey = secertKey.length() > 24 ? secertKey.substring(0, 24) : secertKey;
            return secertKey;
        } else {
            StringBuilder builder = new StringBuilder();
            if (ConfigConstant.devPSK != null && ConfigConstant.devPSK.length() != 0) { //动态注册, 从DevPriv中读取
                builder.append(ConfigConstant.devPSK);
            } else { //证书认证，从证书文件中读取
                return null;
            }
            String privateKey = builder.toString();
            if (privateKey.contains("-----BEGIN PRIVATE KEY-----")) {
                secertKey = privateKey;
            } else {
                secertKey = null;
//                    mParent.printLogInfo(TAG, "Invaild Private Key File.", mLogInfoText);
            }
        }
        return secertKey;
    }

    @Override
    public void printDebug(String message) {
//            mParent.printLogInfo(TAG, message, mLogInfoText);
        //TXLog.d(TAG,message);
    }

    @Override
    public boolean saveLogOffline(String log) {
        //判断SD卡是否可用
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//                mParent.printLogInfo(TAG, "saveLogOffline not ready", mLogInfoText);
            return false;
        }
//
//            String logFilePath = mLogPath + mProductID + mDevName + ".log";
//
////            TXLog.i(TAG, "Save log to %s", logFilePath);
//
//            try {
//                BufferedWriter wLog = new BufferedWriter(new FileWriter(new File(logFilePath), true));
//                wLog.write(log);
//                wLog.flush();
//                wLog.close();
//                return true;
//            } catch (IOException e) {
//                String logInfo = String.format("Save log to [%s] failed, check the Storage permission!", logFilePath);
////                mParent.printLogInfo(TAG, logInfo, mLogInfoText);
//                e.printStackTrace();
//                return false;
//            }
        return true;
    }

    @Override
    public String readOfflineLog() {
        //判断SD卡是否可用
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//                mParent.printLogInfo(TAG, "readOfflineLog not ready", mLogInfoText);
            return null;
        }

//            String logFilePath = mLogPath + mProductID + mDevName + ".log";
//
////            TXLog.i(TAG, "Read log from %s", logFilePath);
//
//            try {
//                BufferedReader logReader = new BufferedReader(new FileReader(logFilePath));
//                StringBuilder offlineLog = new StringBuilder();
//                int data;
//                while ((data = logReader.read()) != -1) {
//                    offlineLog.append((char) data);
//                }
//                logReader.close();
//                return offlineLog.toString();
//            } catch (IOException e) {
//                e.printStackTrace();
//                return null;
//            }
        return null;
    }

    @Override
    public boolean delOfflineLog() {

        //判断SD卡是否可用
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//                mParent.printLogInfo(TAG, "delOfflineLog not ready", mLogInfoText);
            return false;
        }

//            String logFilePath = mLogPath + mProductID + mDevName + ".log";
//
//            File file = new File(logFilePath);
//            if (file.exists() && file.isFile()) {
//                if (file.delete()) {
//                    return true;
//                }
//            }
        return false;
    }

}